package com.alone.embedded.lucene.quickstart.spring;

import com.alone.embedded.lucene.repository.handler.AutoFillHandler;
import com.alone.embedded.lucene.repository.handler.DefaultAutoFillValProvider;
import com.alone.embedded.lucene.repository.handler.LogHandler;
import com.alone.embedded.lucene.repository.handler.RepositoryHandler;
import com.alone.embedded.lucene.spring.DefaultFieldTypeAnalyzerMapper;
import com.alone.embedded.lucene.spring.FieldTypeAnalyzerMapper;
import com.alone.embedded.lucene.spring.annotation.EnableEmbeddedLucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Alone
 * @date 2022/8/30 5:18 下午
 */
@EnableEmbeddedLucene
@Configuration(proxyBeanMethods = false)
public class EmbeddedLuceneConfigure {

    /**
     * 仓库增强
     */
    @Bean
    public RepositoryHandler logHandler() {
        return new LogHandler();
    }

    /**
     * 仓库增强
     */
    @Bean
    public RepositoryHandler autoFillHandler() {
        return new AutoFillHandler(new DefaultAutoFillValProvider());
    }

    /**
     * 客制解析器
     */
    @Bean
    public FieldTypeAnalyzerMapper fieldTypeAnalyzerMapper() {
        return new DefaultFieldTypeAnalyzerMapper();
    }

    /**
     * 客制解析器
     */
    @Bean
    public Analyzer articleAnalyzer() {
        IKAnalyzer ikAnalyzer = new IKAnalyzer(true);
        Map<String, Analyzer> fieldAnalyzers = new HashMap<>(8);
        fieldAnalyzers.put("title", ikAnalyzer);
        fieldAnalyzers.put("content", ikAnalyzer);
        fieldAnalyzers.put("abContent", ikAnalyzer);
        fieldAnalyzers.put("author", ikAnalyzer);
        return new PerFieldAnalyzerWrapper(new StandardAnalyzer(), fieldAnalyzers);
    }
}
